home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / comm / tcp / ATCP_src_22.lha / AmiTCP-2.2 / src / amitcp / sys / mbuf.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-12  |  15.8 KB  |  483 lines

  1. /*
  2.  * $Id: mbuf.h,v 1.10 1993/05/04 13:49:01 jraja Exp $
  3.  *
  4.  * HISTORY
  5.  * $Log: mbuf.h,v $
  6.  * Revision 1.10  1993/05/04  13:49:01  jraja
  7.  * Fixed syntax of zero length arrays with SASC.
  8.  *
  9.  * Revision 1.9  93/04/24  22:33:10  22:33:10  jraja (Jarno Tapio Rajahalme)
  10.  * Removed USECLUSTERS (now using clusters always),
  11.  * moved MSIZE from <machine/param.h> to here,
  12.  * added mbconf structure to hold configurable variables for mbufs,
  13.  * changed MCLBYTES from compile-time constant to configurable variable
  14.  * (mbconf.mclbytes), changed buffer of the mcluster to variable size,
  15.  * Added MTCOUNT, the total count of mbuf types, mbstat now has only MTCOUNT
  16.  * entries for the mbuf types,
  17.  * removed m_retryhdr() call from MGETHDR, since MGET already calls m_retry(),
  18.  * removed obsolete (memory pages) related stuff,
  19.  * moved mbuf function prototypes from distinct proto file to here.
  20.  * 
  21.  * Revision 1.8  93/04/23  02:29:43  02:29:43  ppessi (Pekka Pessi)
  22.  * Fixed type of clusterchunk
  23.  * 
  24.  * Revision 1.7  93/04/12  09:21:56  09:21:56  jraja (Jarno Tapio Rajahalme)
  25.  * Corrected MCLALLOC's comment.
  26.  * 
  27.  * Revision 1.6  93/04/06  00:09:08  00:09:08  jraja (Jarno Tapio Rajahalme)
  28.  * Changed spl function return types to spl_t.
  29.  * 
  30.  * Revision 1.5  93/04/02  01:06:50  01:06:50  jraja (Jarno Tapio Rajahalme)
  31.  * Implemented clusters.
  32.  * 
  33.  * Revision 1.4  93/03/03  19:19:31  19:19:31  jraja (Jarno Tapio Rajahalme)
  34.  * Cleanup. Moved definitions defining storage to kern/uipc_mbuf.c.
  35.  * 
  36.  * Revision 1.3  93/03/03  12:30:55  12:30:55  jraja (Jarno Tapio Rajahalme)
  37.  * Changed function prototypes to prototype file inclusion.
  38.  * 
  39.  * Revision 1.2  93/01/06  19:13:27  19:13:27  jraja (Jarno Tapio Rajahalme)
  40.  * Commented all memory clusters related stuff with #ifdef USECLUSTERS and 
  41.  * deleted some obsolete stuff.
  42.  * 
  43.  * Revision 1.1  92/11/20  15:42:12  15:42:12  jraja (Jarno Tapio Rajahalme)
  44.  * Initial revision
  45.  * 
  46.  */
  47.  
  48. /* 
  49.  * Mach Operating System
  50.  * Copyright (c) 1992 Carnegie Mellon University
  51.  * All Rights Reserved.
  52.  * 
  53.  * Permission to use, copy, modify and distribute this software and its
  54.  * documentation is hereby granted, provided that both the copyright
  55.  * notice and this permission notice appear in all copies of the
  56.  * software, derivative works or modified versions, and any portions
  57.  * thereof, and that both notices appear in supporting documentation.
  58.  * 
  59.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  60.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  61.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  62.  * 
  63.  * Carnegie Mellon requests users of this software to return to
  64.  * 
  65.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  66.  *  School of Computer Science
  67.  *  Carnegie Mellon University
  68.  *  Pittsburgh PA 15213-3890
  69.  * 
  70.  * any improvements or extensions that they make and grant Carnegie Mellon 
  71.  * the rights to redistribute these changes.
  72.  */
  73. /*
  74.  * HISTORY
  75.  * Log:    mbuf.h,v
  76.  * Revision 2.2  92/06/25  17:25:50  mrt
  77.  *     Return dtom back to masking.  Allocation routines will now
  78.  *     correctly allign mbufs.
  79.  *     [92/06/23            rwd]
  80.  * 
  81.  * Revision 2.1  92/04/21  17:15:34  rwd
  82.  * BSDSS
  83.  * 
  84.  *
  85.  */
  86.  
  87. /*
  88.  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
  89.  * All rights reserved.
  90.  *
  91.  * Redistribution and use in source and binary forms, with or without
  92.  * modification, are permitted provided that the following conditions
  93.  * are met:
  94.  * 1. Redistributions of source code must retain the above copyright
  95.  *    notice, this list of conditions and the following disclaimer.
  96.  * 2. Redistributions in binary form must reproduce the above copyright
  97.  *    notice, this list of conditions and the following disclaimer in the
  98.  *    documentation and/or other materials provided with the distribution.
  99.  * 3. All advertising materials mentioning features or use of this software
  100.  *    must display the following acknowledgement:
  101.  *    This product includes software developed by the University of
  102.  *    California, Berkeley and its contributors.
  103.  * 4. Neither the name of the University nor the names of its contributors
  104.  *    may be used to endorse or promote products derived from this software
  105.  *    without specific prior written permission.
  106.  *
  107.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  108.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  109.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  110.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  111.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  112.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  113.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  114.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  115.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  116.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  117.  * SUCH DAMAGE.
  118.  *
  119.  *    @(#)mbuf.h    7.14 (Berkeley) 12/5/90
  120.  */
  121.  
  122. #ifndef SYS_MBUF_H
  123. #define SYS_MBUF_H
  124.  
  125. #ifndef SYS_MALLOC_H
  126. #include <sys/malloc.h>
  127. #endif
  128.  
  129. #if 0                /* not needed (yet), DO NOT DELETE! */
  130. extern void mcl_free_routine();
  131. #endif
  132. extern struct mbuf *mfree;
  133.  
  134. /*
  135.  * Mbufs are of a single size, MSIZE, which
  136.  * includes overhead.  An mbuf may add a single "mbuf cluster" of size
  137.  * mbconf.mclbytes (see struct mbconf), which has no additional overhead
  138.  * and is used instead of the internal data area; this is done when
  139.  * at least MINCLSIZE of data must be stored.
  140.  */
  141.  
  142. #define    MSIZE        128                /* size of an mbuf */
  143. #define    MLEN        (MSIZE - sizeof(struct m_hdr))    /* normal data len */
  144. #define    MHLEN        (MLEN - sizeof(struct pkthdr))    /* data len w/pkthdr */
  145.  
  146. #define    MINCLSIZE    (MHLEN + MLEN) /* smallest amount to put in cluster */
  147. #define    M_MAXCOMPRESS    (MHLEN / 2)    /* max amount to copy for compression */
  148.  
  149. /*
  150.  * Mbuf cluster structure. The first word is used as a reference count when the
  151.  * cluster is in use and as a next pointer if on the free list.
  152.  */
  153. struct mcluster {
  154.     union {
  155.         struct mcluster *mcl_next;
  156.         short            mcl_refcnt;    /* reference count */
  157.     } mcl;
  158.     char    mcl_buf[0];    /* variable size (mbconf.mclbytes) */
  159. };
  160.  
  161. /*
  162.  * Macros for type conversion
  163.  * mtod(m,t) -    convert mbuf pointer to data pointer of correct type
  164.  * dtom(x) -    convert data pointer within mbuf to mbuf pointer (XXX)
  165.  *              WARNING: cannot be used with clusters
  166.  */
  167. #define mtod(m,t)    ((t)((m)->m_data))
  168. #define    dtom(x)        ((struct mbuf *)((int)(x) & ~(MSIZE-1)))
  169.  
  170. /* header at beginning of each mbuf: */
  171. struct m_hdr {
  172.     struct    mbuf *mh_next;        /* next buffer in chain */
  173.     struct    mbuf *mh_nextpkt;    /* next chain in queue/record */
  174.     int    mh_len;            /* amount of data in this mbuf */
  175.     caddr_t    mh_data;        /* location of data */
  176.     short    mh_type;        /* type of data in this mbuf */
  177.     short    mh_flags;        /* flags; see below */
  178. };
  179.  
  180. /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
  181. struct    pkthdr {
  182.     int    len;        /* total packet length */
  183.     struct    ifnet *rcvif;    /* rcv interface */
  184. };
  185.  
  186. /* description of external storage mapped into mbuf, valid if M_EXT set */
  187. struct m_ext {
  188.     struct mcluster *ext_buf;    /* external buffer */
  189. #if 0                    /* Not used in AmiTCP/IP */
  190.     void    (*ext_free)();        /* free routine if not the usual */
  191. #endif
  192.     u_int    ext_size;        /* size of buffer, for ext_free */
  193. };
  194.  
  195. struct mbuf {
  196.     struct    m_hdr m_hdr;
  197.     union {
  198.         struct {
  199.             struct    pkthdr MH_pkthdr;    /* M_PKTHDR set */
  200.             union {
  201.                 struct    m_ext MH_ext;    /* M_EXT set */
  202.                 char    MH_databuf[MHLEN];
  203.             } MH_dat;
  204.         } MH;
  205.         char    M_databuf[MLEN];        /* !M_PKTHDR, !M_EXT */
  206.     } M_dat;
  207. };
  208. #define    m_next        m_hdr.mh_next
  209. #define    m_len        m_hdr.mh_len
  210. #define    m_data        m_hdr.mh_data
  211. #define    m_type        m_hdr.mh_type
  212. #define    m_flags        m_hdr.mh_flags
  213. #define    m_nextpkt    m_hdr.mh_nextpkt
  214. #define    m_act        m_nextpkt
  215. #define    m_pkthdr    M_dat.MH.MH_pkthdr
  216. #define    m_ext        M_dat.MH.MH_dat.MH_ext
  217. #define    m_pktdat    M_dat.MH.MH_dat.MH_databuf
  218. #define    m_dat        M_dat.M_databuf
  219.  
  220. /* mbuf flags */
  221. #define    M_EXT        0x0001    /* has associated external storage */
  222. #define    M_PKTHDR    0x0002    /* start of record */
  223. #define    M_EOR        0x0004    /* end of record */
  224.  
  225. /* mbuf pkthdr flags, also in m_flags */
  226. #define    M_BCAST        0x0100    /* send/received as link-level broadcast */
  227. #define    M_MCAST        0x0200    /* send/received as link-level multicast */
  228.  
  229. /* flags copied when copying m_pkthdr */
  230. #define    M_COPYFLAGS    (M_PKTHDR|M_EOR|M_BCAST|M_MCAST)
  231.  
  232. /* mbuf types */
  233. #define    MT_FREE        0    /* should be on free list */
  234. #define    MT_DATA        1    /* dynamic (data) allocation */
  235. #define    MT_HEADER    2    /* packet header */
  236. #define    MT_SOCKET    3    /* socket structure */
  237. #define    MT_PCB        4    /* protocol control block */
  238. #define    MT_RTABLE    5    /* routing tables */
  239. #define    MT_HTABLE    6    /* IMP host tables */
  240. #define    MT_ATABLE    7    /* address resolution tables */
  241. #define    MT_SONAME    8    /* socket name */
  242. #define    MT_SOOPTS    10    /* socket options */
  243. #define    MT_FTABLE    11    /* fragment reassembly header */
  244. #define    MT_RIGHTS    12    /* access rights */
  245. #define    MT_IFADDR    13    /* interface address */
  246. #define MT_CONTROL    14    /* extra-data protocol message */
  247. #define MT_OOBDATA    15    /* expedited data  */
  248. #define MTCOUNT         16    /* TOTAL COUNT OF THE TYPES */
  249.  
  250. /* flags to m_get/MGET */
  251. #define    M_DONTWAIT    M_NOWAIT
  252. #define    M_WAIT        M_WAITOK
  253.  
  254. /*
  255.  * mbuf allocation/deallocation macros:
  256.  *
  257.  *    MGET(struct mbuf *m, int canwait, int type)
  258.  * allocates an mbuf and initializes it to contain internal data.
  259.  *
  260.  *    MGETHDR(struct mbuf *m, int canwait, int type)
  261.  * allocates an mbuf and initializes it to contain a packet header
  262.  * and internal data.
  263.  */
  264. #define    MGET(m, canwait, type) { \
  265.     spl_t ms = splimp(); \
  266.         (m) = mfree; \
  267.     if (m) { \
  268.         mfree = (m)->m_next; \
  269.         (m)->m_type = (type); \
  270.         mbstat.m_mtypes[type]++; \
  271.         (m)->m_next = NULL; \
  272.         (m)->m_nextpkt = NULL; \
  273.         (m)->m_data = (m)->m_dat; \
  274.         (m)->m_flags = 0; \
  275.     } else \
  276.         (m) = m_retry((canwait), (type)); \
  277.     splx(ms); \
  278. }
  279.  
  280. #define    MGETHDR(m, canwait, type) { \
  281.     MGET(m, canwait, type) \
  282.     if (m) { \
  283.             (m)->m_data = (m)->m_pktdat; \
  284.             (m)->m_flags = M_PKTHDR; \
  285.     } \
  286. }
  287.  
  288. /*
  289.  * Mbuf cluster macros.
  290.  * MCLALLOC(struct mcluster *p, int canwait) allocates an mbuf cluster.
  291.  * MCLGET adds such clusters to a normal mbuf;
  292.  * the flag M_EXT is set upon success.
  293.  * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
  294.  * freeing the cluster if the reference count has reached 0.
  295.  */
  296.  
  297. #define    MCLALLOC(p, canwait) \
  298.     { spl_t ms = splimp(); \
  299.       if (mclfree == 0) \
  300.         (void)m_clalloc(mbconf.clusterchunk, (canwait)); \
  301.       if ((p) = mclfree) { \
  302.         mbstat.m_clfree--; \
  303.         mclfree = (p)->mcl.mcl_next; \
  304.         (p)->mcl.mcl_refcnt = 1; \
  305.       } \
  306.       splx(ms); \
  307.     }
  308.  
  309. #define    MCLGET(m, canwait) \
  310.     { MCLALLOC((m)->m_ext.ext_buf, (canwait)); \
  311.       if ((m)->m_ext.ext_buf != NULL) { \
  312.         (m)->m_data = (m)->m_ext.ext_buf->mcl_buf; \
  313.         (m)->m_flags |= M_EXT; \
  314.         (m)->m_ext.ext_size = mbconf.mclbytes; \
  315. /*            (m)->m_ext.ext_free = mcl_free_routine; */ \
  316.       } \
  317.     }
  318.  
  319. #define    MCLFREE(p) \
  320.     { spl_t ms = splimp(); \
  321.       if (--((p)->mcl.mcl_refcnt) == 0) { \
  322.         (p)->mcl.mcl_next = mclfree; \
  323.         mclfree = (p); \
  324.         mbstat.m_clfree++; \
  325.       } \
  326.       splx(ms); \
  327.     }
  328.  
  329. /*
  330.  * MFREE(struct mbuf *m, struct mbuf *n)
  331.  * Free a single mbuf and associated external storage.
  332.  * Place the successor, if any, in n.
  333.  */
  334. #define    MFREE(m, n) \
  335.     { spl_t ms = splimp(); \
  336.       mbstat.m_mtypes[(m)->m_type]--; \
  337.       if ((m)->m_flags & M_EXT) { \
  338. /*        if ((m)->m_ext.ext_free) */ \
  339. /*            (*((m)->m_ext.ext_free))((m)->m_ext.ext_buf, */ \
  340. /*                (m)->m_ext.ext_size); */ \
  341. /*        else */ \
  342.             MCLFREE((m)->m_ext.ext_buf); \
  343.       } \
  344.       (n) = (m)->m_next; \
  345.       (m)->m_next = mfree; mfree = (m); \
  346.       splx(ms); \
  347.     }
  348.  
  349. /*
  350.  * Copy mbuf pkthdr from from to to.
  351.  * from must have M_PKTHDR set, and to must be empty.
  352.  */
  353. #define    M_COPY_PKTHDR(to, from) { \
  354.     (to)->m_pkthdr = (from)->m_pkthdr; \
  355.     (to)->m_flags = (from)->m_flags & M_COPYFLAGS; \
  356.     (to)->m_data = (to)->m_pktdat; \
  357. }
  358.  
  359. /*
  360.  * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
  361.  * an object of the specified size at the end of the mbuf, longword aligned.
  362.  */
  363. #define    M_ALIGN(m, len) \
  364.     { (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); }
  365. /*
  366.  * As above, for mbufs allocated with m_gethdr/MGETHDR
  367.  * or initialized by M_COPY_PKTHDR.
  368.  */
  369. #define    MH_ALIGN(m, len) \
  370.     { (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); }
  371.  
  372. /*
  373.  * Compute the amount of space available
  374.  * before the current start of data in an mbuf.
  375.  */
  376. #define    M_LEADINGSPACE(m) \
  377.         ((m)->m_flags & M_EXT ? /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \
  378.         (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
  379.         (m)->m_data - (m)->m_dat)
  380.  
  381. /*
  382.  * Compute the amount of space available
  383.  * after the end of data in an mbuf.
  384.  */
  385. #define    M_TRAILINGSPACE(m) \
  386.     ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf->mcl_buf + (m)->m_ext.ext_size - \
  387.         ((m)->m_data + (m)->m_len) : \
  388.         &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
  389.  
  390. /*
  391.  * Arrange to prepend space of size plen to mbuf m.
  392.  * If a new mbuf must be allocated, canwait specifies whether to wait.
  393.  * If canwait is M_DONTWAIT and allocation fails, the original mbuf chain
  394.  * is freed and m is set to NULL.
  395.  */
  396. #define    M_PREPEND(m, plen, canwait) { \
  397.     if (M_LEADINGSPACE(m) >= (plen)) { \
  398.         (m)->m_data -= (plen); \
  399.         (m)->m_len += (plen); \
  400.     } else \
  401.         (m) = m_prepend((m), (plen), (canwait)); \
  402.     if ((m) && (m)->m_flags & M_PKTHDR) \
  403.         (m)->m_pkthdr.len += (plen); \
  404. }
  405.  
  406. /* change mbuf to new type */
  407. #define MCHTYPE(m, t) { \
  408.     mbstat.m_mtypes[(m)->m_type]--; \
  409.     mbstat.m_mtypes[t]++; \
  410.     (m)->m_type = t;\
  411. }
  412.  
  413. /* length to m_copy to copy all */
  414. #define    M_COPYALL    1000000000
  415.  
  416. /* compatiblity with 4.3 */
  417. #define  m_copy(m, o, l)    m_copym((m), (o), (l), M_DONTWAIT)
  418.  
  419. /*
  420.  * Configurable variables. These are put in a structure to ensure that they
  421.  * will be in sequence, since these are accessed as an array of u_longs
  422.  * from the <kern/amiga_config.c>.
  423.  * NOTE: <kern/amiga_config.c> depends on the order of these values.
  424.  */
  425. struct mbconf {
  426.   u_long initial_mbuf_chunks;   /* # of mbuf chunks to allocate initially */
  427.   u_long mbufchunk;         /* # of mbufs to allocate at a time */
  428.   u_long clusterchunk;        /* # of clusters to allocate at a time */
  429.   u_long maxmem;        /* maximum memory to use (in kilobytes) */
  430.   u_long mclbytes;        /* size of the mbuf cluster */
  431. };
  432.  
  433. /*
  434.  * Mbuf statistics.
  435.  */
  436. struct mbstat {
  437.     u_long    m_mbufs;    /* mbufs obtained from page pool */
  438.     u_long    m_clusters;    /* clusters obtained from page pool */
  439.     u_long    m_clfree;    /* free clusters */
  440.     u_long    m_drops;    /* times failed to find space */
  441.     u_long    m_wait;        /* times waited for space */
  442.     u_long    m_drain;    /* times drained protocols for space */
  443.     u_long  m_memused;    /* total amount of memory used for mbufs */
  444.     u_short    m_mtypes[MTCOUNT];    /* type specific mbuf allocations */
  445. };
  446.  
  447. #ifdef    KERNEL
  448. /*
  449.  * changed definitions to external declarations, storege is now defined
  450.  * in kern/uipc_mbuf.c
  451.  */
  452. extern struct mcluster *mclfree;
  453. extern struct mbconf   mbconf;
  454. extern struct mbstat   mbstat;
  455. extern int             max_linkhdr;    /* largest link-level header */
  456. extern int             max_protohdr;    /* largest protocol header */
  457. extern int             max_hdr;        /* largest link+protocol header */
  458. extern int             max_datalen;    /* MHLEN - max_hdr */
  459.  
  460. int mb_check_conf(void *dp, LONG newvalue);
  461.  
  462. BOOL mbinit(void);
  463. void mbdeinit(void);
  464. BOOL m_alloc(int howmany, int canwait);
  465. BOOL m_clalloc(int ncl, int canwait);
  466. struct mbuf * m_retry(int canwait, int type);
  467. void m_reclaim(void);
  468. struct mbuf * m_get(int canwait, int type);
  469. struct mbuf * m_gethdr(int canwait, int type);
  470. struct mbuf * m_getclr(int canwait, int type);
  471. struct mbuf * m_free(struct mbuf * m);
  472. void m_freem(struct mbuf * m);
  473. struct mbuf * m_prepend(struct mbuf * m, int len, int canwait);
  474. struct mbuf * m_copym(struct mbuf * m, int off0, int len, int wait);
  475. void m_copydata(struct mbuf * m, int off, int len, caddr_t cp);
  476. void m_cat(struct mbuf * m, struct mbuf * n);
  477. void m_adj(struct mbuf * mp, int req_len);
  478. struct mbuf * m_pullup(struct mbuf * n, int len);
  479.  
  480. #endif /* KERNEL */
  481.  
  482. #endif /* !SYS_MBUF_H */
  483.